{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "9fd54a32",
   "metadata": {},
   "source": [
    "<a href=\"https://colab.research.google.com/github/run-llama/llama_index/blob/main/docs/examples/llm/openai.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9e3a8796-edc8-43f2-94ad-fe4fb20d70ed",
   "metadata": {},
   "source": [
    "# AlibabaCloud-PaiEas"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "081d07d2",
   "metadata": {},
   "source": [
    "If you're opening this Notebook on colab, you will probably need to install LlamaIndex 🦙."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3f6f8702",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Requirement already satisfied: llama-index-llms-paieas in /mnt/llm/xiaowen/miniconda3/envs/llama_env/lib/python3.9/site-packages (0.1.16)\n",
      "Requirement already satisfied: llama-index-core<0.11.0,>=0.10.57 in /mnt/llm/xiaowen/miniconda3/envs/llama_env/lib/python3.9/site-packages (from llama-index-llms-paieas) (0.10.58)\n",
      "Requirement already satisfied: llama-index-llms-openai<0.2.0,>=0.1.1 in /mnt/llm/xiaowen/miniconda3/envs/llama_env/lib/python3.9/site-packages (from llama-index-llms-paieas) (0.1.27)\n",
      "Requirement already satisfied: PyYAML>=6.0.1 in /mnt/llm/xiaowen/miniconda3/envs/llama_env/lib/python3.9/site-packages (from llama-index-core<0.11.0,>=0.10.57->llama-index-llms-paieas) (6.0.1)\n",
      "Requirement already satisfied: SQLAlchemy>=1.4.49 in /mnt/llm/xiaowen/miniconda3/envs/llama_env/lib/python3.9/site-packages (from SQLAlchemy[asyncio]>=1.4.49->llama-index-core<0.11.0,>=0.10.57->llama-index-llms-paieas) (2.0.31)\n",
      "Requirement already satisfied: aiohttp<4.0.0,>=3.8.6 in /mnt/llm/xiaowen/miniconda3/envs/llama_env/lib/python3.9/site-packages (from llama-index-core<0.11.0,>=0.10.57->llama-index-llms-paieas) (3.9.5)\n",
      "Requirement already satisfied: dataclasses-json in /mnt/llm/xiaowen/miniconda3/envs/llama_env/lib/python3.9/site-packages (from llama-index-core<0.11.0,>=0.10.57->llama-index-llms-paieas) (0.6.7)\n",
      "Requirement already satisfied: deprecated>=1.2.9.3 in /mnt/llm/xiaowen/miniconda3/envs/llama_env/lib/python3.9/site-packages (from llama-index-core<0.11.0,>=0.10.57->llama-index-llms-paieas) (1.2.14)\n",
      "Requirement already satisfied: dirtyjson<2.0.0,>=1.0.8 in /mnt/llm/xiaowen/miniconda3/envs/llama_env/lib/python3.9/site-packages (from llama-index-core<0.11.0,>=0.10.57->llama-index-llms-paieas) (1.0.8)\n",
      "Requirement already satisfied: fsspec>=2023.5.0 in /mnt/llm/xiaowen/miniconda3/envs/llama_env/lib/python3.9/site-packages (from llama-index-core<0.11.0,>=0.10.57->llama-index-llms-paieas) (2024.6.1)\n",
      "Requirement already satisfied: httpx in /mnt/llm/xiaowen/miniconda3/envs/llama_env/lib/python3.9/site-packages (from llama-index-core<0.11.0,>=0.10.57->llama-index-llms-paieas) (0.27.0)\n",
      "Requirement already satisfied: nest-asyncio<2.0.0,>=1.5.8 in /mnt/llm/xiaowen/miniconda3/envs/llama_env/lib/python3.9/site-packages (from llama-index-core<0.11.0,>=0.10.57->llama-index-llms-paieas) (1.6.0)\n",
      "Requirement already satisfied: networkx>=3.0 in /mnt/llm/xiaowen/miniconda3/envs/llama_env/lib/python3.9/site-packages (from llama-index-core<0.11.0,>=0.10.57->llama-index-llms-paieas) (3.1)\n",
      "Requirement already satisfied: nltk<4.0.0,>=3.8.1 in /mnt/llm/xiaowen/miniconda3/envs/llama_env/lib/python3.9/site-packages (from llama-index-core<0.11.0,>=0.10.57->llama-index-llms-paieas) (3.8.1)\n",
      "Requirement already satisfied: numpy<2.0.0 in /mnt/llm/xiaowen/miniconda3/envs/llama_env/lib/python3.9/site-packages (from llama-index-core<0.11.0,>=0.10.57->llama-index-llms-paieas) (1.24.4)\n",
      "Requirement already satisfied: openai>=1.1.0 in /mnt/llm/xiaowen/miniconda3/envs/llama_env/lib/python3.9/site-packages (from llama-index-core<0.11.0,>=0.10.57->llama-index-llms-paieas) (1.37.1)\n",
      "Requirement already satisfied: pandas in /mnt/llm/xiaowen/miniconda3/envs/llama_env/lib/python3.9/site-packages (from llama-index-core<0.11.0,>=0.10.57->llama-index-llms-paieas) (2.0.3)\n",
      "Requirement already satisfied: pillow>=9.0.0 in /mnt/llm/xiaowen/miniconda3/envs/llama_env/lib/python3.9/site-packages (from llama-index-core<0.11.0,>=0.10.57->llama-index-llms-paieas) (10.4.0)\n",
      "Requirement already satisfied: requests>=2.31.0 in /mnt/llm/xiaowen/miniconda3/envs/llama_env/lib/python3.9/site-packages (from llama-index-core<0.11.0,>=0.10.57->llama-index-llms-paieas) (2.32.3)\n",
      "Requirement already satisfied: tenacity!=8.4.0,<9.0.0,>=8.2.0 in /mnt/llm/xiaowen/miniconda3/envs/llama_env/lib/python3.9/site-packages (from llama-index-core<0.11.0,>=0.10.57->llama-index-llms-paieas) (8.5.0)\n",
      "Requirement already satisfied: tiktoken>=0.3.3 in /mnt/llm/xiaowen/miniconda3/envs/llama_env/lib/python3.9/site-packages (from llama-index-core<0.11.0,>=0.10.57->llama-index-llms-paieas) (0.7.0)\n",
      "Requirement already satisfied: tqdm<5.0.0,>=4.66.1 in /mnt/llm/xiaowen/miniconda3/envs/llama_env/lib/python3.9/site-packages (from llama-index-core<0.11.0,>=0.10.57->llama-index-llms-paieas) (4.66.4)\n",
      "Requirement already satisfied: typing-extensions>=4.5.0 in /mnt/llm/xiaowen/miniconda3/envs/llama_env/lib/python3.9/site-packages (from llama-index-core<0.11.0,>=0.10.57->llama-index-llms-paieas) (4.12.2)\n",
      "Requirement already satisfied: typing-inspect>=0.8.0 in /mnt/llm/xiaowen/miniconda3/envs/llama_env/lib/python3.9/site-packages (from llama-index-core<0.11.0,>=0.10.57->llama-index-llms-paieas) (0.9.0)\n",
      "Requirement already satisfied: wrapt in /mnt/llm/xiaowen/miniconda3/envs/llama_env/lib/python3.9/site-packages (from llama-index-core<0.11.0,>=0.10.57->llama-index-llms-paieas) (1.16.0)\n",
      "Requirement already satisfied: aiosignal>=1.1.2 in /mnt/llm/xiaowen/miniconda3/envs/llama_env/lib/python3.9/site-packages (from aiohttp<4.0.0,>=3.8.6->llama-index-core<0.11.0,>=0.10.57->llama-index-llms-paieas) (1.3.1)\n",
      "Requirement already satisfied: attrs>=17.3.0 in /mnt/llm/xiaowen/miniconda3/envs/llama_env/lib/python3.9/site-packages (from aiohttp<4.0.0,>=3.8.6->llama-index-core<0.11.0,>=0.10.57->llama-index-llms-paieas) (23.2.0)\n",
      "Requirement already satisfied: frozenlist>=1.1.1 in /mnt/llm/xiaowen/miniconda3/envs/llama_env/lib/python3.9/site-packages (from aiohttp<4.0.0,>=3.8.6->llama-index-core<0.11.0,>=0.10.57->llama-index-llms-paieas) (1.4.1)\n",
      "Requirement already satisfied: multidict<7.0,>=4.5 in /mnt/llm/xiaowen/miniconda3/envs/llama_env/lib/python3.9/site-packages (from aiohttp<4.0.0,>=3.8.6->llama-index-core<0.11.0,>=0.10.57->llama-index-llms-paieas) (6.0.5)\n",
      "Requirement already satisfied: yarl<2.0,>=1.0 in /mnt/llm/xiaowen/miniconda3/envs/llama_env/lib/python3.9/site-packages (from aiohttp<4.0.0,>=3.8.6->llama-index-core<0.11.0,>=0.10.57->llama-index-llms-paieas) (1.9.4)\n",
      "Requirement already satisfied: async-timeout<5.0,>=4.0 in /mnt/llm/xiaowen/miniconda3/envs/llama_env/lib/python3.9/site-packages (from aiohttp<4.0.0,>=3.8.6->llama-index-core<0.11.0,>=0.10.57->llama-index-llms-paieas) (4.0.3)\n",
      "Requirement already satisfied: click in /mnt/llm/xiaowen/miniconda3/envs/llama_env/lib/python3.9/site-packages (from nltk<4.0.0,>=3.8.1->llama-index-core<0.11.0,>=0.10.57->llama-index-llms-paieas) (8.1.7)\n",
      "Requirement already satisfied: joblib in /mnt/llm/xiaowen/miniconda3/envs/llama_env/lib/python3.9/site-packages (from nltk<4.0.0,>=3.8.1->llama-index-core<0.11.0,>=0.10.57->llama-index-llms-paieas) (1.4.2)\n",
      "Requirement already satisfied: regex>=2021.8.3 in /mnt/llm/xiaowen/miniconda3/envs/llama_env/lib/python3.9/site-packages (from nltk<4.0.0,>=3.8.1->llama-index-core<0.11.0,>=0.10.57->llama-index-llms-paieas) (2024.7.24)\n",
      "Requirement already satisfied: anyio<5,>=3.5.0 in /mnt/llm/xiaowen/miniconda3/envs/llama_env/lib/python3.9/site-packages (from openai>=1.1.0->llama-index-core<0.11.0,>=0.10.57->llama-index-llms-paieas) (4.4.0)\n",
      "Requirement already satisfied: distro<2,>=1.7.0 in /mnt/llm/xiaowen/miniconda3/envs/llama_env/lib/python3.9/site-packages (from openai>=1.1.0->llama-index-core<0.11.0,>=0.10.57->llama-index-llms-paieas) (1.9.0)\n",
      "Requirement already satisfied: pydantic<3,>=1.9.0 in /mnt/llm/xiaowen/miniconda3/envs/llama_env/lib/python3.9/site-packages (from openai>=1.1.0->llama-index-core<0.11.0,>=0.10.57->llama-index-llms-paieas) (2.8.2)\n",
      "Requirement already satisfied: sniffio in /mnt/llm/xiaowen/miniconda3/envs/llama_env/lib/python3.9/site-packages (from openai>=1.1.0->llama-index-core<0.11.0,>=0.10.57->llama-index-llms-paieas) (1.3.1)\n",
      "Requirement already satisfied: certifi in /mnt/llm/xiaowen/miniconda3/envs/llama_env/lib/python3.9/site-packages (from httpx->llama-index-core<0.11.0,>=0.10.57->llama-index-llms-paieas) (2024.7.4)\n",
      "Requirement already satisfied: httpcore==1.* in /mnt/llm/xiaowen/miniconda3/envs/llama_env/lib/python3.9/site-packages (from httpx->llama-index-core<0.11.0,>=0.10.57->llama-index-llms-paieas) (1.0.5)\n",
      "Requirement already satisfied: idna in /mnt/llm/xiaowen/miniconda3/envs/llama_env/lib/python3.9/site-packages (from httpx->llama-index-core<0.11.0,>=0.10.57->llama-index-llms-paieas) (3.7)\n",
      "Requirement already satisfied: h11<0.15,>=0.13 in /mnt/llm/xiaowen/miniconda3/envs/llama_env/lib/python3.9/site-packages (from httpcore==1.*->httpx->llama-index-core<0.11.0,>=0.10.57->llama-index-llms-paieas) (0.14.0)\n",
      "Requirement already satisfied: charset-normalizer<4,>=2 in /mnt/llm/xiaowen/miniconda3/envs/llama_env/lib/python3.9/site-packages (from requests>=2.31.0->llama-index-core<0.11.0,>=0.10.57->llama-index-llms-paieas) (3.3.2)\n",
      "Requirement already satisfied: urllib3<3,>=1.21.1 in /mnt/llm/xiaowen/miniconda3/envs/llama_env/lib/python3.9/site-packages (from requests>=2.31.0->llama-index-core<0.11.0,>=0.10.57->llama-index-llms-paieas) (2.2.2)\n",
      "Requirement already satisfied: greenlet!=0.4.17 in /mnt/llm/xiaowen/miniconda3/envs/llama_env/lib/python3.9/site-packages (from SQLAlchemy>=1.4.49->SQLAlchemy[asyncio]>=1.4.49->llama-index-core<0.11.0,>=0.10.57->llama-index-llms-paieas) (3.0.3)\n",
      "Requirement already satisfied: mypy-extensions>=0.3.0 in /mnt/llm/xiaowen/miniconda3/envs/llama_env/lib/python3.9/site-packages (from typing-inspect>=0.8.0->llama-index-core<0.11.0,>=0.10.57->llama-index-llms-paieas) (1.0.0)\n",
      "Requirement already satisfied: marshmallow<4.0.0,>=3.18.0 in /mnt/llm/xiaowen/miniconda3/envs/llama_env/lib/python3.9/site-packages (from dataclasses-json->llama-index-core<0.11.0,>=0.10.57->llama-index-llms-paieas) (3.21.3)\n",
      "Requirement already satisfied: python-dateutil>=2.8.2 in /mnt/llm/xiaowen/miniconda3/envs/llama_env/lib/python3.9/site-packages (from pandas->llama-index-core<0.11.0,>=0.10.57->llama-index-llms-paieas) (2.9.0.post0)\n",
      "Requirement already satisfied: pytz>=2020.1 in /mnt/llm/xiaowen/miniconda3/envs/llama_env/lib/python3.9/site-packages (from pandas->llama-index-core<0.11.0,>=0.10.57->llama-index-llms-paieas) (2024.1)\n",
      "Requirement already satisfied: tzdata>=2022.1 in /mnt/llm/xiaowen/miniconda3/envs/llama_env/lib/python3.9/site-packages (from pandas->llama-index-core<0.11.0,>=0.10.57->llama-index-llms-paieas) (2024.1)\n",
      "Requirement already satisfied: exceptiongroup>=1.0.2 in /mnt/llm/xiaowen/miniconda3/envs/llama_env/lib/python3.9/site-packages (from anyio<5,>=3.5.0->openai>=1.1.0->llama-index-core<0.11.0,>=0.10.57->llama-index-llms-paieas) (1.2.2)\n",
      "Requirement already satisfied: packaging>=17.0 in /mnt/llm/xiaowen/miniconda3/envs/llama_env/lib/python3.9/site-packages (from marshmallow<4.0.0,>=3.18.0->dataclasses-json->llama-index-core<0.11.0,>=0.10.57->llama-index-llms-paieas) (24.1)\n",
      "Requirement already satisfied: annotated-types>=0.4.0 in /mnt/llm/xiaowen/miniconda3/envs/llama_env/lib/python3.9/site-packages (from pydantic<3,>=1.9.0->openai>=1.1.0->llama-index-core<0.11.0,>=0.10.57->llama-index-llms-paieas) (0.7.0)\n",
      "Requirement already satisfied: pydantic-core==2.20.1 in /mnt/llm/xiaowen/miniconda3/envs/llama_env/lib/python3.9/site-packages (from pydantic<3,>=1.9.0->openai>=1.1.0->llama-index-core<0.11.0,>=0.10.57->llama-index-llms-paieas) (2.20.1)\n",
      "Requirement already satisfied: six>=1.5 in /mnt/llm/xiaowen/miniconda3/envs/llama_env/lib/python3.9/site-packages (from python-dateutil>=2.8.2->pandas->llama-index-core<0.11.0,>=0.10.57->llama-index-llms-paieas) (1.16.0)\n",
      "Note: you may need to restart the kernel to use updated packages.\n"
     ]
    }
   ],
   "source": [
    "%pip install llama-index-llms-paieas"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "83ea30ee",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Collecting llama-index\n",
      "  Downloading llama_index-0.10.58-py3-none-any.whl.metadata (11 kB)\n",
      "Collecting llama-index-agent-openai<0.3.0,>=0.1.4 (from llama-index)\n",
      "  Downloading llama_index_agent_openai-0.2.9-py3-none-any.whl.metadata (729 bytes)\n",
      "Collecting llama-index-cli<0.2.0,>=0.1.2 (from llama-index)\n",
      "  Downloading llama_index_cli-0.1.13-py3-none-any.whl.metadata (1.5 kB)\n",
      "Requirement already satisfied: llama-index-core==0.10.58 in /mnt/llm/xiaowen/miniconda3/envs/llama_env/lib/python3.9/site-packages (from llama-index) (0.10.58)\n",
      "Collecting llama-index-embeddings-openai<0.2.0,>=0.1.5 (from llama-index)\n",
      "  Downloading llama_index_embeddings_openai-0.1.11-py3-none-any.whl.metadata (655 bytes)\n",
      "Collecting llama-index-indices-managed-llama-cloud>=0.2.0 (from llama-index)\n",
      "  Downloading llama_index_indices_managed_llama_cloud-0.2.7-py3-none-any.whl.metadata (3.8 kB)\n",
      "Collecting llama-index-legacy<0.10.0,>=0.9.48 (from llama-index)\n",
      "  Downloading llama_index_legacy-0.9.48-py3-none-any.whl.metadata (8.5 kB)\n",
      "Requirement already satisfied: llama-index-llms-openai<0.2.0,>=0.1.27 in /mnt/llm/xiaowen/miniconda3/envs/llama_env/lib/python3.9/site-packages (from llama-index) (0.1.27)\n",
      "Collecting llama-index-multi-modal-llms-openai<0.2.0,>=0.1.3 (from llama-index)\n",
      "  Downloading llama_index_multi_modal_llms_openai-0.1.8-py3-none-any.whl.metadata (728 bytes)\n",
      "Collecting llama-index-program-openai<0.2.0,>=0.1.3 (from llama-index)\n",
      "  Downloading llama_index_program_openai-0.1.7-py3-none-any.whl.metadata (760 bytes)\n",
      "Collecting llama-index-question-gen-openai<0.2.0,>=0.1.2 (from llama-index)\n",
      "  Downloading llama_index_question_gen_openai-0.1.3-py3-none-any.whl.metadata (785 bytes)\n",
      "Collecting llama-index-readers-file<0.2.0,>=0.1.4 (from llama-index)\n",
      "  Downloading llama_index_readers_file-0.1.31-py3-none-any.whl.metadata (5.4 kB)\n",
      "Collecting llama-index-readers-llama-parse>=0.1.2 (from llama-index)\n",
      "  Downloading llama_index_readers_llama_parse-0.1.6-py3-none-any.whl.metadata (3.6 kB)\n",
      "Requirement already satisfied: PyYAML>=6.0.1 in /mnt/llm/xiaowen/miniconda3/envs/llama_env/lib/python3.9/site-packages (from llama-index-core==0.10.58->llama-index) (6.0.1)\n",
      "Requirement already satisfied: SQLAlchemy>=1.4.49 in /mnt/llm/xiaowen/miniconda3/envs/llama_env/lib/python3.9/site-packages (from SQLAlchemy[asyncio]>=1.4.49->llama-index-core==0.10.58->llama-index) (2.0.31)\n",
      "Requirement already satisfied: aiohttp<4.0.0,>=3.8.6 in /mnt/llm/xiaowen/miniconda3/envs/llama_env/lib/python3.9/site-packages (from llama-index-core==0.10.58->llama-index) (3.9.5)\n",
      "Requirement already satisfied: dataclasses-json in /mnt/llm/xiaowen/miniconda3/envs/llama_env/lib/python3.9/site-packages (from llama-index-core==0.10.58->llama-index) (0.6.7)\n",
      "Requirement already satisfied: deprecated>=1.2.9.3 in /mnt/llm/xiaowen/miniconda3/envs/llama_env/lib/python3.9/site-packages (from llama-index-core==0.10.58->llama-index) (1.2.14)\n",
      "Requirement already satisfied: dirtyjson<2.0.0,>=1.0.8 in /mnt/llm/xiaowen/miniconda3/envs/llama_env/lib/python3.9/site-packages (from llama-index-core==0.10.58->llama-index) (1.0.8)\n",
      "Requirement already satisfied: fsspec>=2023.5.0 in /mnt/llm/xiaowen/miniconda3/envs/llama_env/lib/python3.9/site-packages (from llama-index-core==0.10.58->llama-index) (2024.6.1)\n",
      "Requirement already satisfied: httpx in /mnt/llm/xiaowen/miniconda3/envs/llama_env/lib/python3.9/site-packages (from llama-index-core==0.10.58->llama-index) (0.27.0)\n",
      "Requirement already satisfied: nest-asyncio<2.0.0,>=1.5.8 in /mnt/llm/xiaowen/miniconda3/envs/llama_env/lib/python3.9/site-packages (from llama-index-core==0.10.58->llama-index) (1.6.0)\n",
      "Requirement already satisfied: networkx>=3.0 in /mnt/llm/xiaowen/miniconda3/envs/llama_env/lib/python3.9/site-packages (from llama-index-core==0.10.58->llama-index) (3.1)\n",
      "Requirement already satisfied: nltk<4.0.0,>=3.8.1 in /mnt/llm/xiaowen/miniconda3/envs/llama_env/lib/python3.9/site-packages (from llama-index-core==0.10.58->llama-index) (3.8.1)\n",
      "Requirement already satisfied: numpy<2.0.0 in /mnt/llm/xiaowen/miniconda3/envs/llama_env/lib/python3.9/site-packages (from llama-index-core==0.10.58->llama-index) (1.24.4)\n",
      "Requirement already satisfied: openai>=1.1.0 in /mnt/llm/xiaowen/miniconda3/envs/llama_env/lib/python3.9/site-packages (from llama-index-core==0.10.58->llama-index) (1.37.1)\n",
      "Requirement already satisfied: pandas in /mnt/llm/xiaowen/miniconda3/envs/llama_env/lib/python3.9/site-packages (from llama-index-core==0.10.58->llama-index) (2.0.3)\n",
      "Requirement already satisfied: pillow>=9.0.0 in /mnt/llm/xiaowen/miniconda3/envs/llama_env/lib/python3.9/site-packages (from llama-index-core==0.10.58->llama-index) (10.4.0)\n",
      "Requirement already satisfied: requests>=2.31.0 in /mnt/llm/xiaowen/miniconda3/envs/llama_env/lib/python3.9/site-packages (from llama-index-core==0.10.58->llama-index) (2.32.3)\n",
      "Requirement already satisfied: tenacity!=8.4.0,<9.0.0,>=8.2.0 in /mnt/llm/xiaowen/miniconda3/envs/llama_env/lib/python3.9/site-packages (from llama-index-core==0.10.58->llama-index) (8.5.0)\n",
      "Requirement already satisfied: tiktoken>=0.3.3 in /mnt/llm/xiaowen/miniconda3/envs/llama_env/lib/python3.9/site-packages (from llama-index-core==0.10.58->llama-index) (0.7.0)\n",
      "Requirement already satisfied: tqdm<5.0.0,>=4.66.1 in /mnt/llm/xiaowen/miniconda3/envs/llama_env/lib/python3.9/site-packages (from llama-index-core==0.10.58->llama-index) (4.66.4)\n",
      "Requirement already satisfied: typing-extensions>=4.5.0 in /mnt/llm/xiaowen/miniconda3/envs/llama_env/lib/python3.9/site-packages (from llama-index-core==0.10.58->llama-index) (4.12.2)\n",
      "Requirement already satisfied: typing-inspect>=0.8.0 in /mnt/llm/xiaowen/miniconda3/envs/llama_env/lib/python3.9/site-packages (from llama-index-core==0.10.58->llama-index) (0.9.0)\n",
      "Requirement already satisfied: wrapt in /mnt/llm/xiaowen/miniconda3/envs/llama_env/lib/python3.9/site-packages (from llama-index-core==0.10.58->llama-index) (1.16.0)\n",
      "Collecting llama-cloud>=0.0.11 (from llama-index-indices-managed-llama-cloud>=0.2.0->llama-index)\n",
      "  Downloading llama_cloud-0.0.11-py3-none-any.whl.metadata (751 bytes)\n",
      "Requirement already satisfied: beautifulsoup4<5.0.0,>=4.12.3 in /mnt/llm/xiaowen/miniconda3/envs/llama_env/lib/python3.9/site-packages (from llama-index-readers-file<0.2.0,>=0.1.4->llama-index) (4.12.3)\n",
      "Collecting pypdf<5.0.0,>=4.0.1 (from llama-index-readers-file<0.2.0,>=0.1.4->llama-index)\n",
      "  Downloading pypdf-4.3.1-py3-none-any.whl.metadata (7.4 kB)\n",
      "Collecting striprtf<0.0.27,>=0.0.26 (from llama-index-readers-file<0.2.0,>=0.1.4->llama-index)\n",
      "  Downloading striprtf-0.0.26-py3-none-any.whl.metadata (2.1 kB)\n",
      "Collecting llama-parse>=0.4.0 (from llama-index-readers-llama-parse>=0.1.2->llama-index)\n",
      "  Downloading llama_parse-0.4.9-py3-none-any.whl.metadata (4.4 kB)\n",
      "Requirement already satisfied: aiosignal>=1.1.2 in /mnt/llm/xiaowen/miniconda3/envs/llama_env/lib/python3.9/site-packages (from aiohttp<4.0.0,>=3.8.6->llama-index-core==0.10.58->llama-index) (1.3.1)\n",
      "Requirement already satisfied: attrs>=17.3.0 in /mnt/llm/xiaowen/miniconda3/envs/llama_env/lib/python3.9/site-packages (from aiohttp<4.0.0,>=3.8.6->llama-index-core==0.10.58->llama-index) (23.2.0)\n",
      "Requirement already satisfied: frozenlist>=1.1.1 in /mnt/llm/xiaowen/miniconda3/envs/llama_env/lib/python3.9/site-packages (from aiohttp<4.0.0,>=3.8.6->llama-index-core==0.10.58->llama-index) (1.4.1)\n",
      "Requirement already satisfied: multidict<7.0,>=4.5 in /mnt/llm/xiaowen/miniconda3/envs/llama_env/lib/python3.9/site-packages (from aiohttp<4.0.0,>=3.8.6->llama-index-core==0.10.58->llama-index) (6.0.5)\n",
      "Requirement already satisfied: yarl<2.0,>=1.0 in /mnt/llm/xiaowen/miniconda3/envs/llama_env/lib/python3.9/site-packages (from aiohttp<4.0.0,>=3.8.6->llama-index-core==0.10.58->llama-index) (1.9.4)\n",
      "Requirement already satisfied: async-timeout<5.0,>=4.0 in /mnt/llm/xiaowen/miniconda3/envs/llama_env/lib/python3.9/site-packages (from aiohttp<4.0.0,>=3.8.6->llama-index-core==0.10.58->llama-index) (4.0.3)\n",
      "Requirement already satisfied: soupsieve>1.2 in /mnt/llm/xiaowen/miniconda3/envs/llama_env/lib/python3.9/site-packages (from beautifulsoup4<5.0.0,>=4.12.3->llama-index-readers-file<0.2.0,>=0.1.4->llama-index) (2.5)\n",
      "Requirement already satisfied: pydantic>=1.10 in /mnt/llm/xiaowen/miniconda3/envs/llama_env/lib/python3.9/site-packages (from llama-cloud>=0.0.11->llama-index-indices-managed-llama-cloud>=0.2.0->llama-index) (2.8.2)\n",
      "Requirement already satisfied: anyio in /mnt/llm/xiaowen/miniconda3/envs/llama_env/lib/python3.9/site-packages (from httpx->llama-index-core==0.10.58->llama-index) (4.4.0)\n",
      "Requirement already satisfied: certifi in /mnt/llm/xiaowen/miniconda3/envs/llama_env/lib/python3.9/site-packages (from httpx->llama-index-core==0.10.58->llama-index) (2024.7.4)\n",
      "Requirement already satisfied: httpcore==1.* in /mnt/llm/xiaowen/miniconda3/envs/llama_env/lib/python3.9/site-packages (from httpx->llama-index-core==0.10.58->llama-index) (1.0.5)\n",
      "Requirement already satisfied: idna in /mnt/llm/xiaowen/miniconda3/envs/llama_env/lib/python3.9/site-packages (from httpx->llama-index-core==0.10.58->llama-index) (3.7)\n",
      "Requirement already satisfied: sniffio in /mnt/llm/xiaowen/miniconda3/envs/llama_env/lib/python3.9/site-packages (from httpx->llama-index-core==0.10.58->llama-index) (1.3.1)\n",
      "Requirement already satisfied: h11<0.15,>=0.13 in /mnt/llm/xiaowen/miniconda3/envs/llama_env/lib/python3.9/site-packages (from httpcore==1.*->httpx->llama-index-core==0.10.58->llama-index) (0.14.0)\n",
      "Requirement already satisfied: click in /mnt/llm/xiaowen/miniconda3/envs/llama_env/lib/python3.9/site-packages (from nltk<4.0.0,>=3.8.1->llama-index-core==0.10.58->llama-index) (8.1.7)\n",
      "Requirement already satisfied: joblib in /mnt/llm/xiaowen/miniconda3/envs/llama_env/lib/python3.9/site-packages (from nltk<4.0.0,>=3.8.1->llama-index-core==0.10.58->llama-index) (1.4.2)\n",
      "Requirement already satisfied: regex>=2021.8.3 in /mnt/llm/xiaowen/miniconda3/envs/llama_env/lib/python3.9/site-packages (from nltk<4.0.0,>=3.8.1->llama-index-core==0.10.58->llama-index) (2024.7.24)\n",
      "Requirement already satisfied: distro<2,>=1.7.0 in /mnt/llm/xiaowen/miniconda3/envs/llama_env/lib/python3.9/site-packages (from openai>=1.1.0->llama-index-core==0.10.58->llama-index) (1.9.0)\n",
      "Requirement already satisfied: charset-normalizer<4,>=2 in /mnt/llm/xiaowen/miniconda3/envs/llama_env/lib/python3.9/site-packages (from requests>=2.31.0->llama-index-core==0.10.58->llama-index) (3.3.2)\n",
      "Requirement already satisfied: urllib3<3,>=1.21.1 in /mnt/llm/xiaowen/miniconda3/envs/llama_env/lib/python3.9/site-packages (from requests>=2.31.0->llama-index-core==0.10.58->llama-index) (2.2.2)\n",
      "Requirement already satisfied: greenlet!=0.4.17 in /mnt/llm/xiaowen/miniconda3/envs/llama_env/lib/python3.9/site-packages (from SQLAlchemy>=1.4.49->SQLAlchemy[asyncio]>=1.4.49->llama-index-core==0.10.58->llama-index) (3.0.3)\n",
      "Requirement already satisfied: mypy-extensions>=0.3.0 in /mnt/llm/xiaowen/miniconda3/envs/llama_env/lib/python3.9/site-packages (from typing-inspect>=0.8.0->llama-index-core==0.10.58->llama-index) (1.0.0)\n",
      "Requirement already satisfied: marshmallow<4.0.0,>=3.18.0 in /mnt/llm/xiaowen/miniconda3/envs/llama_env/lib/python3.9/site-packages (from dataclasses-json->llama-index-core==0.10.58->llama-index) (3.21.3)\n",
      "Requirement already satisfied: python-dateutil>=2.8.2 in /mnt/llm/xiaowen/miniconda3/envs/llama_env/lib/python3.9/site-packages (from pandas->llama-index-core==0.10.58->llama-index) (2.9.0.post0)\n",
      "Requirement already satisfied: pytz>=2020.1 in /mnt/llm/xiaowen/miniconda3/envs/llama_env/lib/python3.9/site-packages (from pandas->llama-index-core==0.10.58->llama-index) (2024.1)\n",
      "Requirement already satisfied: tzdata>=2022.1 in /mnt/llm/xiaowen/miniconda3/envs/llama_env/lib/python3.9/site-packages (from pandas->llama-index-core==0.10.58->llama-index) (2024.1)\n",
      "Requirement already satisfied: exceptiongroup>=1.0.2 in /mnt/llm/xiaowen/miniconda3/envs/llama_env/lib/python3.9/site-packages (from anyio->httpx->llama-index-core==0.10.58->llama-index) (1.2.2)\n",
      "Requirement already satisfied: packaging>=17.0 in /mnt/llm/xiaowen/miniconda3/envs/llama_env/lib/python3.9/site-packages (from marshmallow<4.0.0,>=3.18.0->dataclasses-json->llama-index-core==0.10.58->llama-index) (24.1)\n",
      "Requirement already satisfied: annotated-types>=0.4.0 in /mnt/llm/xiaowen/miniconda3/envs/llama_env/lib/python3.9/site-packages (from pydantic>=1.10->llama-cloud>=0.0.11->llama-index-indices-managed-llama-cloud>=0.2.0->llama-index) (0.7.0)\n",
      "Requirement already satisfied: pydantic-core==2.20.1 in /mnt/llm/xiaowen/miniconda3/envs/llama_env/lib/python3.9/site-packages (from pydantic>=1.10->llama-cloud>=0.0.11->llama-index-indices-managed-llama-cloud>=0.2.0->llama-index) (2.20.1)\n",
      "Requirement already satisfied: six>=1.5 in /mnt/llm/xiaowen/miniconda3/envs/llama_env/lib/python3.9/site-packages (from python-dateutil>=2.8.2->pandas->llama-index-core==0.10.58->llama-index) (1.16.0)\n",
      "Downloading llama_index-0.10.58-py3-none-any.whl (6.8 kB)\n",
      "Downloading llama_index_agent_openai-0.2.9-py3-none-any.whl (13 kB)\n",
      "Downloading llama_index_cli-0.1.13-py3-none-any.whl (27 kB)\n",
      "Downloading llama_index_embeddings_openai-0.1.11-py3-none-any.whl (6.3 kB)\n",
      "Downloading llama_index_indices_managed_llama_cloud-0.2.7-py3-none-any.whl (9.5 kB)\n",
      "Downloading llama_index_legacy-0.9.48-py3-none-any.whl (2.0 MB)\n",
      "\u001b[2K   \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m2.0/2.0 MB\u001b[0m \u001b[31m42.6 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m:00:01\u001b[0m\n",
      "\u001b[?25hDownloading llama_index_multi_modal_llms_openai-0.1.8-py3-none-any.whl (5.9 kB)\n",
      "Downloading llama_index_program_openai-0.1.7-py3-none-any.whl (5.3 kB)\n",
      "Downloading llama_index_question_gen_openai-0.1.3-py3-none-any.whl (2.9 kB)\n",
      "Downloading llama_index_readers_file-0.1.31-py3-none-any.whl (38 kB)\n",
      "Downloading llama_index_readers_llama_parse-0.1.6-py3-none-any.whl (2.5 kB)\n",
      "Downloading llama_cloud-0.0.11-py3-none-any.whl (154 kB)\n",
      "\u001b[2K   \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m154.8/154.8 kB\u001b[0m \u001b[31m5.8 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
      "\u001b[?25hDownloading llama_parse-0.4.9-py3-none-any.whl (9.4 kB)\n",
      "Downloading pypdf-4.3.1-py3-none-any.whl (295 kB)\n",
      "\u001b[2K   \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m295.8/295.8 kB\u001b[0m \u001b[31m12.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
      "\u001b[?25hDownloading striprtf-0.0.26-py3-none-any.whl (6.9 kB)\n",
      "Installing collected packages: striprtf, pypdf, llama-cloud, llama-index-legacy, llama-parse, llama-index-readers-file, llama-index-indices-managed-llama-cloud, llama-index-embeddings-openai, llama-index-readers-llama-parse, llama-index-multi-modal-llms-openai, llama-index-cli, llama-index-agent-openai, llama-index-program-openai, llama-index-question-gen-openai, llama-index\n",
      "Successfully installed llama-cloud-0.0.11 llama-index-0.10.58 llama-index-agent-openai-0.2.9 llama-index-cli-0.1.13 llama-index-embeddings-openai-0.1.11 llama-index-indices-managed-llama-cloud-0.2.7 llama-index-legacy-0.9.48 llama-index-multi-modal-llms-openai-0.1.8 llama-index-program-openai-0.1.7 llama-index-question-gen-openai-0.1.3 llama-index-readers-file-0.1.31 llama-index-readers-llama-parse-0.1.6 llama-parse-0.4.9 pypdf-4.3.1 striprtf-0.0.26\n"
     ]
    }
   ],
   "source": [
    "!pip install llama-index"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b007403c-6b7a-420c-92f1-4171d05ed9bb",
   "metadata": {},
   "source": [
    "## Basic Usage\n",
    "\n",
    "You will need to get an API key and url from [AlibabaCloud PAI Eas](https://help.aliyun.com/zh/pai/use-cases/deploy-llm-in-eas?spm=5176.pai-console-inland.help.dexternal.107e642dLd2e9J). Once you have one, you can either pass it explicity to the model, or use the `PAIEAS_API_KEY` and `PAIEAS_API_BASE` environment variable."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2e7b148c",
   "metadata": {},
   "outputs": [],
   "source": [
    "!export PAIEAS_API_KEY=your_service_token\n",
    "!export PAIEAS_API_BASE=your_access_address"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8ead155e-b8bd-46f9-ab9b-28fc009361dd",
   "metadata": {},
   "source": [
    "#### Call `complete` with a prompt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "60be18ae-c957-4ac2-a58a-0652e18ee6d6",
   "metadata": {},
   "outputs": [],
   "source": [
    "from llama_index.llms.paieas import PaiEas\n",
    "\n",
    "llm = PaiEas()\n",
    "resp = llm.complete(\"Write a poem about a magic backpack\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b9274443",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "In a land of wonder and delight, Where dreams and magic come to light, There lived a young wanderer, with a heart full of glee, And in his backpack, a secret did he hold, A magic item, a treasure to see.\n",
      "This backpack, oh how it shimmered and glowed, With an enchantment that did flow, A subtle power, hidden from the eye, But to the owner, it was a sight so high.\n",
      "Its weight was light, yet held a weight of its own, As if it carried stories yet unknown, It could shrink when empty, grow when full of good, A true companion, a faithful tool.\n",
      "With a simple rustle, it spoke in a hush, Guiding the youth on his journey rough, It would expand to carry mountains, or carry a breeze, Its magic knew no bounds, no limits to please.\n",
      "Inside, it held treasures beyond measure, Secrets from the universe, indeed, A compass for lost directions, a lamp in the dark, A friend in need, a magic backpack's mark.\n",
      "It helped the wanderer through forests, across rivers wide, Assisted him in times of strife and pride, It taught him lessons, showed him the way, And in its embrace, he felt okay.\n",
      "So here's to the magic backpack, a wondrous thing, To the young traveler, it's a symphony of wings, A magic that exists, not just in our minds, But in the world, where the explorers find.\n"
     ]
    }
   ],
   "source": [
    "print(resp)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "14831268-f90f-499d-9d86-925dbc88292b",
   "metadata": {},
   "source": [
    "#### Call `chat` with a list of messages"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bbe29574-4af1-48d5-9739-f60652b6ce6c",
   "metadata": {},
   "outputs": [],
   "source": [
    "from llama_index.core.llms import ChatMessage\n",
    "\n",
    "messages = [\n",
    "    ChatMessage(\n",
    "        role=\"system\", content=\"You are a pirate with a colorful personality\"\n",
    "    ),\n",
    "    ChatMessage(role=\"user\", content=\"What is your name\"),\n",
    "]\n",
    "resp = llm.chat(messages)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5eb3f2e5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "assistant: As a pirate, I be known as Jack \"Sparrow\" Silverhand, or simply Jack the Pirate. Ye can call me that if ye be feelin' adventurous!\n"
     ]
    }
   ],
   "source": [
    "print(resp)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2ed5e894-4597-4911-a623-591560f72b82",
   "metadata": {},
   "source": [
    "## Streaming"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4cb7986f-aaed-42e2-abdd-f274f6d4fc59",
   "metadata": {},
   "source": [
    "Using `stream_complete` endpoint"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d43f17a2-0aeb-464b-a7a7-732ba5e8ef24",
   "metadata": {},
   "outputs": [],
   "source": [
    "resp = llm.stream_complete(\"Paul Graham is \")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9aef6d37",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Paul Graham is a computer scientist, entrepreneur, and investor. He was born on June 24, 1955, in New York City. Graham co-founded the online coding platform HackerRank, the startup venture firm Y Combinator, and Viaweb, which later became Yahoo! GeoMaps. He is also known for his influential writing, including the book \"Hackers & Painters\" and his blog, where he shares insights on technology, startups, and entrepreneurship. Graham is considered a prominent figure in the tech industry and is often cited for his early contributions to the growth of the Silicon Valley ecosystem."
     ]
    }
   ],
   "source": [
    "for r in resp:\n",
    "    print(r.delta, end=\"\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "40350dd8-3f50-4a2f-8545-5723942039bb",
   "metadata": {},
   "source": [
    "Using `stream_chat` endpoint"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bc636e65-a67b-4dcd-ac60-b25abc9d8dbd",
   "metadata": {},
   "outputs": [],
   "source": [
    "from llama_index.core.llms import ChatMessage\n",
    "\n",
    "messages = [\n",
    "    ChatMessage(\n",
    "        role=\"system\", content=\"You are a pirate with a colorful personality\"\n",
    "    ),\n",
    "    ChatMessage(role=\"user\", content=\"What is your name\"),\n",
    "]\n",
    "resp = llm.stream_chat(messages)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6df2e7ce",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "As a pirate, I don't have a fancy name like a landlubber might. I go by the fearsome moniker \"Blackbeard\" or \"Captain Jack\" to strike fear into the hearts of my enemies and justify my swashbuckling ways. But remember, I'm just a friendly AI here to chat, not to cause any real harm!"
     ]
    }
   ],
   "source": [
    "for r in resp:\n",
    "    print(r.delta, end=\"\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "df5fa1ab-f598-46da-80f3-f6af5dd2fe83",
   "metadata": {},
   "source": [
    "## Async"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8f649683-896a-439e-b14b-e5df5d803b82",
   "metadata": {},
   "outputs": [],
   "source": [
    "resp = await llm.acomplete(\"Paul Graham is \")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cfef2f68",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Paul Graham is a renowned entrepreneur, computer scientist, and venture capitalist. Born on April 24, 1973, in Massachusetts, USA, he is best known for co-founding Y Combinator, a startup accelerator that has fueled many successful companies such as Airbnb, Dropbox, and GitHub. Graham is also the author of several influential books, including \"Hackers and Painters\" and \"Startup School,\" which provide insights into his views on entrepreneurship and technology. He has been a significant figure in the tech industry and is often seen as a thought leader in the field.\n"
     ]
    }
   ],
   "source": [
    "print(resp)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "abc72d09-3bcd-4d48-9bb7-0920c56310c1",
   "metadata": {},
   "outputs": [],
   "source": [
    "resp = await llm.astream_complete(\"Paul Graham is \")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9c9696ea",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Paul Graham is an American entrepreneur, computer scientist, and writer. He co-founded the software company Y Combinator, which is a well-known startup accelerator and investment firm. Graham is also known for his blog, \"Hackers & Painters,\" where he shares his thoughts on various topics including entrepreneurship, technology, and education. He has written several influential books, including \"Hackers: Heroes of the Computer Age\" and \"Why You Can't Build a Startup on Your Living Room Couch.\" Graham has been influential in the tech industry and is considered a prominent figure in the startup ecosystem."
     ]
    }
   ],
   "source": [
    "async for delta in resp:\n",
    "    print(delta.delta, end=\"\")"
   ]
  }
 ],
 "metadata": {
  "colab": {
   "provenance": []
  },
  "kernelspec": {
   "display_name": "llama_env",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
